#------ Makefile; ida examples

# vpath %.cpp . # 源代码的查找目录
OUTPUT_DIR = build
OUTPUT_DIR_ch5 = build/ch5
# my_build_dir = build/built.txt
# vpath %.cpp .
# vpath %.cpp ./ch5
# vpath %.o ./$(OUTPUT_DIR)
# vpath %.o ./$(OUTPUT_DIR_ch5)
# vpath %.exe ./$(OUTPUT_DIR)
# vpath %.exe ./$(OUTPUT_DIR_ch5)
# VPATH = .:./$(OUTPUT_DIR):./$(OUTPUT_DIR_ch5)

# ----- gcc 选项
# CFLAGS := -fopenmp -lm -Og -g -std=c++17
# CFLAGS := -fopenmp -lm -std=c++17 -O3
CFLAGS := -fopenmp  -lm -std=c++17 -O3 -ffast-math

src:=$(wildcard *.cpp **/*.cpp) # 源代码文件

obj_files := $(patsubst %.cpp, $(OUTPUT_DIR)/%.o, $(src)) # .o 目标文件
exe_files := $(patsubst %.cpp, $(OUTPUT_DIR)/%.exe, $(src)) # .exe 可执行文件


# 伪目标
.PHONY : clean all test
all: my_build_dir $(exe_files)

test: ; $(info $$obj_files is [${obj_files}])echo Hello world
test2: ; $(info $$exe_files is [${exe_files}])echo Hello world


# --- xxx.cpp -> xxx.o
$(obj_files): $(OUTPUT_DIR)/%.o:%.cpp
	$(CXX) -c $(CFLAGS) $< -o $@

# --- xxx.o -> xxx.exe
$(exe_files): %.exe:%.o
	$(CXX) $< $(CFLAGS) -o $@

# 构建目录
my_build_dir:
	@[ -d $(OUTPUT_DIR) ] || mkdir -p $(OUTPUT_DIR)
	@[ -d $(OUTPUT_DIR_ch5) ] || mkdir -p $(OUTPUT_DIR_ch5)

#清理
clean:
	-rm build/* -rf
	# -rm $(my_anchor)
	# -rm *.o *.exe